<template>
    <view class="pub_page pub_mine_info pub_header_padding">
        <pub-head headerName="mine_info" :headerConfig="headerConfig"></pub-head>
        <div class="pub_page_scroll page_box">
            <div v-if="openType==1">
                <div class="headimg" @click="chooseImgBefore">
                    <pub-image class="img" :src="groupAvatar"></pub-image>
                    <div class="p">修改群头像</div>
                </div>
                <br>
                <br>
                <br>
                <div class="pub_inputs">
                    <div class="abox">
                        <div class="ali ali_align_right">
                            <div class="atitle">群昵称</div>
                            <input class="ainput" type="text" v-model="groupName" maxlength="15">
                        </div>
                    </div>
                </div>
            </div>
            <div v-if="openType==2">
                <div class="pub_inputs">
                    <div class="abox">
                        <div class="ali">
                            <div class="atitle">群公告</div>
                            <textarea style="height:400rpx" class="atextarea" :placeholder="ownerID==userInfo.userId?'请输入':'暂无公告信息'"
                                v-model="notification" maxlength="130"
                                :disabled="ownerID==userInfo.userId?false:true"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div v-if="openType==3">
                <div class="pub_inputs">
                    <div class="abox">
                        <div class="ali">
                            <div class="atitle">群介绍</div>
                            <textarea style="height:400rpx" class="atextarea" :placeholder="ownerID==userInfo.userId?'请输入':'暂无群介绍信息'"
                                v-model="introduction" maxlength="100"
                                :disabled="ownerID==userInfo.userId?false:true"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <br>
            <br>
            <div v-if="ownerID==userInfo.userId" class="pub_btn" @click="saveInfo">确定保存</div>
        </div>
    </view>
</template>

<script>
export default {
    data() {
        return {
            headerConfig: {
                title: '',//标题栏内容
                left_type: 3,
            },
            userInfo: '',
            ownerID: '',

            groupName: '',
            groupAvatar: '',
            groupId: '',

            openType: 0,//群信息类型  1基础信息（群头像和群昵称）  2群公告  3群介绍
            openTypeName: ['', '基础信息', '群公告', '群介绍'],


            introduction: '',//群介绍
            notification: '',//群公告
        };
    },
    onLoad(options) {
        this.openType = Number(options.openType) || 1;
        const groupInfo = options.groupInfo ? JSON.parse(options.groupInfo) : '';
        console.log(groupInfo);
        this.headerConfig.title = this.openTypeName[this.openType];
        if (groupInfo == '') return;
        this.userInfo = this.$comFun.getUserInfo();
        this.groupId = groupInfo.groupId;
        this.ownerID = groupInfo.ownerID;
        if (this.openType == 1) {
            this.groupName = groupInfo.groupName;
            this.groupAvatar = groupInfo.groupAvatar;
        }
        if (this.openType == 2) {
            this.notification = groupInfo.notification;
        }
        if (this.openType == 3) {
            this.introduction = groupInfo.introduction;
        }
    },
    methods: {
        async saveInfo() {
            const that = this;
            let params = {};
            let groupProfile = {};
            switch (that.openType) {
                case 1:
                    if (!that.groupName.trim()) {
                        that.$comFun.toast('群昵称不能为空');
                        return;
                    }
                    params = {
                        groupAvatar: that.groupAvatar,
                        groupName: that.groupName,
                    };
                    groupProfile = {
                        name: that.groupName, // 修改群名称
                        avatar: that.groupAvatar, // 修改群头像
                    }
                    break;
                case 2:
                    if (!that.notification.trim()) {
                        that.$comFun.toast('群公告不能为空');
                        return;
                    }
                    params = {
                        announcement: that.notification,
                    };
                    groupProfile = {
                        notification: that.notification,
                    }
                    break;
                case 3:
                    if (!that.introduction.trim()) {
                        that.$comFun.toast('群简介不能为空');
                        return;
                    }
                    params = {
                        groupNotice: that.introduction,
                    };
                    groupProfile = {
                        introduction: that.introduction,
                    }
                    break;
                default:
                    break;
            }
            this.sureSaveInfo(params, groupProfile);
        },
        async sureSaveInfo(params, groupProfile) {
            const that = this;
            that.$comFun.openLoading();
            const res_1 = await that.$request.post('chat/updateGroup', {
                ...params,
                groupNumber: that.groupId
            });
            console.log(res_1);
            that.$comFun.closeLoading();
            if (res_1.code == 200) {
                let promise = that.$txChat.updateGroupProfile({
                    groupID: that.groupId,
                    ...groupProfile,
                });
                promise.then(function (imResponse) {
                    console.log(imResponse.data.group) // 修改成功后的群组详细资料

                    that.$comFun.toast('修改成功');
                    uni.$emit("dealGroupInfo", {
                        dealType: that.openType,
                        data: groupProfile
                    })
                    setTimeout(() => {
                        that.$comFun.closeWin();
                    }, 1000)
                }).catch(function (imError) {
                    console.warn('updateGroupProfile error:', imError); // 修改群组资料失败的相关信息
                });
            } else {
                that.$comFun.toast(res_1.message);
            }
        },
        chooseImgBefore() {
            const that = this;
            uni.showModal({
                title: '权限申请',
                content: '用于图片上传，需要申请相机和相册的权限',
                success: function (res) {
                    if (res.confirm) {
                        that.chooseImg();
                    }
                }
            });
        },
        async chooseImg() {
            const that = this;
            getApp().imgUpload(1, (list) => {
                // console.log('list:', list);
                that.groupAvatar = list[0];
            })
        }
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/mine.scss";
</style>
